package com.leihao.product.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.leihao.common.core.exception.ServiceException;
import com.leihao.common.core.utils.StringUtils;
import com.leihao.product.domian.*;
import com.leihao.product.domian.listRequest.BrandListRequest;
import com.leihao.product.mapper.ProductBrandMapper;
import com.leihao.product.service.ProductBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductBrandServiceImpl implements ProductBrandService {


    @Autowired
    private ProductBrandMapper productBrandMapper;


    @Override
    public PageInfo<ProductBrand> findList(BrandListRequest brandListRequest) {
        if (StringUtils.isNull(brandListRequest)) {
            throw new ServiceException("参数异常!");
        }
        PageHelper.startPage(brandListRequest.getPageNum(), brandListRequest.getPageSize());
        List<ProductBrand> list = productBrandMapper.findList(brandListRequest);
        PageInfo<ProductBrand> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public ProductBrand findById(Integer productBrandId) {
        if (productBrandId == null) {
            throw new ServiceException("参数异常!");
        }
        ProductBrand brand = productBrandMapper.findById(productBrandId);
        if (brand == null){
            throw new ServiceException("商品品牌不存在!");
        }
        return brand;
    }

    @Override
    public void delete(Integer productBrandId) {
        if (productBrandId==null){
            throw new ServiceException("参数异常!");
        }
        productBrandMapper.delete(productBrandId);
    }

    @Override
    public void update(ProductBrand productBrand) {
        if (productBrand.getProductBrandId()==null){
            throw new ServiceException("参数异常!");
        }
        if (StringUtils.isNull(productBrand)){
            throw new ServiceException("参数异常!");
        }
        productBrandMapper.update(productBrand);

    }

    @Override
    public void add(ProductBrand productBrand) {
        if (StringUtils.isNull(productBrand)){
            throw new ServiceException("参数异常!");
        }
        productBrandMapper.add(productBrand);
    }

    @Override
    public List<ProductBrand> findAll() {
        return productBrandMapper.findAll();
    }
}
